/**
 * 
 */
package org.dragonfire.util;

import java.util.TreeMap;

/**
 * @author Darth Android
 * @date Aug 26, 2007
 */
public class TimeProfile implements Comparable<TimeProfile> {

	private static TreeMap<String, TimeProfile> profiler = new TreeMap<String, TimeProfile>();

	public static void addRun(String name, long duration) {
		name = name.toUpperCase();
		if (!profiler.containsKey(name))
			profiler.put(name, new TimeProfile(name));
		profiler.get(name).addRun(duration);
	}

	public static TimeProfile[] getProfiles() {
		return profiler.values().toArray(new TimeProfile[profiler.size()]);
	}

	public static TimeProfile getProfile(String name) {
		return profiler.get(name.toUpperCase());
	}

	private long time;
	private int count;
	private String name;

	public TimeProfile(String name) {
		this.name = name;
	}

	public void addRun(long duration) {
		count++;
		time += duration;
	}

	public long getAvgTime() {
		if (getCount() == 0)
			return 0;
		return getTime() / getCount();
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int compareTo(TimeProfile o) {
		return (int) (o.getAvgTime() - getAvgTime());
	}
}
